package com.dave.gd.common.redis.impl.cluster;

import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import redis.clients.util.SafeEncoder;

import com.dave.gd.common.redis.ICacheRepository;
import com.dave.gd.common.util.serial.SerializationUtils;

public class RedisClusterRepositoryImpl implements ICacheRepository {
    
    private BinaryJedisCluster client = JedisClusterFactory.getInstance();
    
    @Override
    public boolean keyExists(String group, String key) {
        return getClient().exists(group + key);
    }
    
    @Override
    public String rename(final String group, final String oldkey, final String newkey) {
        return getClient().set(group + newkey, getClient().getBytes(group + oldkey));
    }
    
    @Override
    public Long delete(final String group, String... keys) {
        final Long r = 0L;
        for (String key : keys) {
            getClient().del(group + key);
        }
        return r;
    }
    
    @Override
    public Long expire(final String group, final String key, final int seconds) {
        return getClient().expire(group + key, seconds);
    }
    
    @Override
    public Long ttl(final String group, final String key) {
        return getClient().ttl(group + key);
    }
    
    @Override
    public Long persist(final String group, final String key) {
        return getClient().persist(group + key);
    }
    
    @Override
    public String set(final String group, final String key, final String value, final int seconds) {
        String r = getClient().set((group + key), value);
        getClient().expire((group + key), seconds);
        return r;
    }
    
    @Override
    public List<String> getMulti(String group, String... keys) {
        String[] newKeys = new String[keys.length];
        for (int i = 0; i < keys.length; i++) {
            newKeys[i] = group + keys[i];
        }
        List<String> result = new ArrayList<String>();
        for (String key : newKeys) {
            result.add(getClient().get(key));
        }
        return result;
    }
    
    @Override
    public List<?> getMultiObject(String group, String... keys) {
        
        List<byte[]> res = new ArrayList<byte[]>();
        for (String key : keys) {
            res.add(getClient().getBytes(group + key));
        }
        
        final List<Object> result = new ArrayList<Object>();
        for (int i = 0; i < res.size(); i++) {
            Object obj = null;
            try {
                obj = SerializationUtils.deserialize(res.get(i));
            } catch (IOException e) {
                e.printStackTrace();
            }
            if (null != obj) {
                result.add(obj);
            } else {
                result.add(SafeEncoder.encode(res.get(i)));
            }
            
        }
        return result;
    }
    
    @Override
    public String setObject(String group, String key, final Serializable value, final int seconds) {
        String r = null;
        try {
            r = getClient().set(group + key, SerializationUtils.serialize(value));
        } catch (IOException e) {
            e.printStackTrace();
        }
        client.expire(group + key, seconds);
        return r;
    }
    
    @Override
    public String setObject(String group, String key, final Serializable value) {
        try {
            return client.set((group + key), SerializationUtils.serialize(value));
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
    
    @Override
    public String set(String group, String key, String value) {
        return getClient().set(group + key, value);
    }
    
    @Override
    public String get(String group, String key) {
        return getClient().get(group + key);
    }
    
    @Override
    public Object getObject(String group, String key) {
        try {
            return SerializationUtils.deserialize(getClient().getBytes(group + key));
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
    
    public BinaryJedisCluster getClient() {
        return client;
    }
    
    public void setClient(BinaryJedisCluster client) {
        this.client = client;
    }
}
